package com.five.welcome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.welcome.entity.Studentinfo;
import com.five.welcome.dao.StudentinfoMapper;
import com.five.welcome.entity.statistic.GenderOfStudentCount;
import com.five.welcome.service.StudentinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ywt
 * @since 2024-07-11
 */
@Service
public class StudentinfoServiceImpl extends ServiceImpl<StudentinfoMapper, Studentinfo> implements StudentinfoService {

    @Autowired
    private StudentinfoMapper studentinfoMapper;

    //注册学生信息
    @Override
    public int addStudent(Studentinfo studentinfo) {
        QueryWrapper<Studentinfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("s_phone",studentinfo.getSPhone());
        if(studentinfoMapper.selectOne(queryWrapper)!=null){
            queryWrapper.clear();
            return 0;
        }
        queryWrapper.clear();
        queryWrapper.eq("s_number",studentinfo.getSNumber());
        if(studentinfoMapper.selectOne(queryWrapper)!=null){
            queryWrapper.clear();
            return 0;
        }
        return studentinfoMapper.insert(studentinfo);
    }

    //通过学号查询学生信息
    @Override
    public Studentinfo getInfoBySNumber(String sNumber) {
        QueryWrapper<Studentinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_number", sNumber);
        return studentinfoMapper.selectOne(queryWrapper);
    }

    //分页条件查询
    @Override
    public Page<Studentinfo> myPage(Integer pageNumber, Integer pageSize,
                                    String sNumber, String major, String classes) {
        Page<Studentinfo> page = Page.of(pageNumber, pageSize);
        QueryWrapper<Studentinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(sNumber != null, "s_number", sNumber)
                .eq(major != null, "major", major)
                .eq(classes != null, "class", classes);
        page(page, queryWrapper);
        return page;
    }

    //通过学号获取名字
    @Override
    public String getSNameBySNumber(String sNumber) {
      return getInfoBySNumber(sNumber).getSName();
    }

    @Override
    public List<GenderOfStudentCount> GenderOfStudentCount() {
        return studentinfoMapper.GenderOfStudentCount();
    }
}
